package com.example.designpatterns.command.concrete;

import com.example.designpatterns.command.Command;
import com.example.designpatterns.command.receiver.Stereo;

/**
 * StereoOffCommand类 - 音响关闭命令
 */
public class StereoOffCommand implements Command {
    private Stereo stereo;
    private boolean wasOn;
    private int previousVolume;
    private String previousMode;
    
    /**
     * 创建音响关闭命令
     * @param stereo 音响对象
     */
    public StereoOffCommand(Stereo stereo) {
        this.stereo = stereo;
    }
    
    @Override
    public void execute() {
        // 保存当前状态以便撤销
        wasOn = stereo.isOn();
        previousVolume = stereo.getVolume();
        previousMode = stereo.getMode();
        
        stereo.turnOff();
    }
    
    @Override
    public boolean undo() {
        if (wasOn) {
            stereo.turnOn();
            stereo.setVolume(previousVolume);
            stereo.setMode(previousMode);
            return true;
        }
        return false;
    }
    
    @Override
    public String getName() {
        return "关闭" + stereo.getLocation() + "的音响";
    }
} 